package com.logis.ryanrqwork;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.logis.ryanrqwork.dao.WinesShap;
import com.logis.ryanrqwork.daoutil.WinesShapDaoController;
import com.logis.ryanrqwork.ui.activity.PersonalDetailActivity;
import com.logis.ryanrqwork.ui.activity.WinesShapDetailActivity;
import com.logis.ryanrqwork.ui.activity.WinesShapManngerActivity;

import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener {
    private static final String USERICON="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1581768896707&di=ca60516ebc2aab23875fd15111202a39&imgtype=0&src=http%3A%2F%2Fpic4.zhimg.com%2F2057a5a0fac87f124fef1b7cdd0dc6ce_fhd.jpg";
    private ListView listview;
    private List<WinesShap> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = ((ListView) findViewById(R.id.listview));
        findViewById(R.id.button_personDetail).setOnClickListener(this);
        findViewById(R.id.button_wineShapMannger).setOnClickListener(this);
        Glide.with(MainActivity.this).load(USERICON).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(((ImageView) findViewById(R.id.imageview_userIcon)));
        list=WinesShapDaoController.getWinesShap(MainActivity.this);
        listview.setAdapter(new MyBaseAdaper());
        listview.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        startActivity(new Intent(this, WinesShapDetailActivity.class));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button_personDetail:
                //用户详情
                startActivity(new Intent(MainActivity.this, PersonalDetailActivity.class));
                break;
            case R.id.button_wineShapMannger:
                startActivity(new Intent(MainActivity.this, WinesShapManngerActivity.class));
                break;
        }
    }

    class MyBaseAdaper extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder holder=null;
           if(convertView==null){
               convertView=View.inflate(getBaseContext(),R.layout.item_windesshap,null);
               holder=new ViewHolder(convertView);
               convertView.setTag(holder);
           }else
               holder= (ViewHolder) convertView.getTag();
           holder.rationBar_winesStar.setRating(list.get(position).getStars());
           holder.textivew_winesName.setText(list.get(position).getName());
           holder.textview_price.setText(list.get(position).getMinPrice());
           holder.textview_winesLocation.setText(list.get(position).getLocation());
            Log.e("---",list.get(position).getIcon());
            Glide.with(MainActivity.this).load(list.get(position).getIcon()).into(holder.imageview_winesIcon);
            return convertView;
        }
        class ViewHolder{
            private ImageView imageview_winesIcon;
            private TextView textivew_winesName;
            private TextView textview_winesLocation;
            private TextView textview_price;
            private RatingBar rationBar_winesStar;
            public ViewHolder(View parent){
                imageview_winesIcon=parent.findViewById(R.id.imageview_winesIcon);
                textivew_winesName=parent.findViewById(R.id.textivew_winesName);
                textview_winesLocation=parent.findViewById(R.id.textview_winesLocation);
                textview_price=parent.findViewById(R.id.textview_price);
                rationBar_winesStar=parent.findViewById(R.id.rationBar_winesStar);
            }
        }
    }
}
